package com.goods.business.service.imp;

import com.goods.business.converter.ConsumerConverter;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Resource
    private ConsumerMapper consumerMapper;

    //物资去处列表  分页带条件
    @Override
    public PageVO<ConsumerVO> findConsumerList(Map<String, Object> queryMap) {
        Example example = new Example(Consumer.class);
        if (queryMap.get("name") != null && !"".equals(queryMap.get("name"))) {
            example.createCriteria().andEqualTo("name", queryMap.get("name"));
        }
        if (queryMap.get("address") != null && !"".equals(queryMap.get("address"))) {
            example.createCriteria().andEqualTo("address", queryMap.get("address"));
        }
        if (queryMap.get("contact") != null && !"".equals(queryMap.get("contact"))) {
            example.createCriteria().andLike("contact",queryMap.get("contact").toString());
        }
        List<Consumer> consumers = consumerMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(consumers)){
            List<ConsumerVO> consumerVOList = ConsumerConverter.converterToConsumerVOList(consumers);
            String pageSize = (String)queryMap.get("pageSize");
            String pageNum = (String)queryMap.get("pageNum");
            List<Consumer> page = ListPageUtils.page(consumers, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
            return new PageVO<>(page.size(),consumerVOList);
        }
        return new PageVO<>(0L,null);
    }
    //添加物资去处
    @Override
    public void add(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        consumer.setAddress(consumerVO.getAddress());
        consumer.setContact(consumerVO.getContact());
        consumer.setName(consumerVO.getName());
        consumer.setPhone(consumerVO.getPhone());
        consumer.setSort(consumerVO.getSort());
        consumer.setCreateTime(new Date());
        consumer.setModifiedTime(new Date());
        consumerMapper.insert(consumer);
    }
    //编辑物资去处
    @Override
    public ConsumerVO edit(Long id) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer,consumerVO);
        return consumerVO;
    }
    //更新
    @Override
    public void update(Long id,ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO,consumer);
        consumer.setId(id);
        consumer.setModifiedTime(new Date());
        consumerMapper.updateByPrimaryKey(consumer);
    }
    //删除
    @Override
    public void delete(Long id) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        consumerMapper.delete(consumer);
    }
    //通过id获取Consumer
    @Override
    public Consumer getByid(Long consumerId) {
        return consumerMapper.selectByPrimaryKey(consumerId);
    }
    //获取所有已知去向的出库记录
    @Override
    public List<ConsumerVO> findAll() {
        List<Consumer> consumers = consumerMapper.selectAll();
        List<ConsumerVO> consumerVOList = ConsumerConverter.converterToConsumerVOList(consumers);
        return consumerVOList;
    }
}
